package com.shuwen.gcdj.service.statistic;

import com.shuwen.gcdj.bean.conditionMonitoring.request.ShockSensorObjectQueryReq;
import com.shuwen.gcdj.bean.conditionMonitoring.response.StatisticsShockSensorResp;
import com.shuwen.gcdj.bean.sysDictionary.request.SysDictionaryReq;
import com.shuwen.gcdj.bean.sysDictionary.response.SysDictionaryResp;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.entity.statistics.StatisticsShocksensor;
import com.shuwen.gcdj.mapper.mysql.Statistic.StatisticsShocksensorMapper;
import com.shuwen.gcdj.mapper.mysql.sysDictionary.SysDictionaryMapper;
import com.shuwen.gcdj.service.BaseService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 健康评估趋势平稳度
 *
 * @author Yangx
 */
@Service
@AllArgsConstructor
@Slf4j
public class StatisticsShocksensorService extends BaseService<StatisticsShocksensorMapper, StatisticsShocksensor> {
    private final StatisticsShocksensorMapper statisticsShocksensorMapper;
    private final SysDictionaryMapper sysDictionaryMapper;

    public CommonResult<List<StatisticsShockSensorResp>> getStatisticsShockSensorList(ShockSensorObjectQueryReq req) {
        List<StatisticsShockSensorResp> resp = new ArrayList<>();
        SysDictionaryReq dictionaryReq = new SysDictionaryReq();
        //查询振动传感器点位字典配置
        dictionaryReq.setCodes(req.getLocations());
        dictionaryReq.setParentCode(req.getParentCode());
        dictionaryReq.setOperateObject(req.getEquipmentCode());
        List<String> locations = sysDictionaryMapper.getList(dictionaryReq).stream().map(SysDictionaryResp::getValue).map(String::valueOf).collect(Collectors.toList());
        req.setLocations(locations);
        //振动与特征值查询
        //获取当日最新数据
        LocalDate localDate = LocalDate.now();
        req.setYear(localDate.getYear());
        req.setMonth(localDate.getMonthValue());
        req.setDay(localDate.getDayOfMonth());
        List<StatisticsShockSensorResp> shockSensors = statisticsShocksensorMapper.GetList(req);
        List<String> evaluateObjects = shockSensors.stream().map(StatisticsShockSensorResp::getEvaluateObject).distinct().collect(Collectors.toList());
        for (String t : evaluateObjects) {
            List<StatisticsShockSensorResp> shockSensor = shockSensors.stream()
                    .filter(item -> Objects.equals(item.getEvaluateObject(), t) && item.getEvaluateObject() != null && item.getEvaluateObject().length() > 0).collect(Collectors.toList());

            double sensorValueSum = shockSensor.stream().mapToDouble(StatisticsShockSensorResp::getSensorValue).sum();
            double tendencyChartSum = shockSensor.stream().mapToDouble(StatisticsShockSensorResp::getTendencyChart).sum();
            float  sensorValue = sensorValueSum !=0 ? (float) (sensorValueSum/ (long) shockSensor.size()) : 0;
            float tendencyChart =tendencyChartSum !=0 ? (float) (tendencyChartSum/ (long) shockSensor.size()) : 0;
            StatisticsShockSensorResp shockSensorResp = new StatisticsShockSensorResp();
            shockSensorResp.setEvaluateObject(t);
            shockSensorResp.setSensorValue(Float.valueOf(String.format("%.2f", sensorValue)));
            shockSensorResp.setTendencyChart(Float.valueOf(String.format("%.2f",tendencyChart)));
            resp.add(shockSensorResp);
        }
        return CommonResult.success(resp);
    }
}
